home *** CD-ROM | disk | FTP | other *** search
- Path: wormer.fn.net!sysadmin@wormer.fn.net
- From: withheld@keepitpublic.com (Rusty Meathook)
- Newsgroups: comp.lang.c
- Subject: Reusable and Generic Functions
- Date: Sat, 20 Apr 1996 11:09:45 GMT
- Organization: Feist Connections
- Message-ID: <4la9k2$76o@wormer.fn.net>
- NNTP-Posting-Host: mark217.fn.net
- X-Newsreader: Forte Agent .99b.112
-
- Suppose a guy has two linked list structures, such as:
-
- /* Untested code based on previously written tested code :) */
-
- typedef struct foo_tag
- {
- int data;
- struct foo_tag *next;
- } FOO;
-
- typedef struct bar_tag
- {
- float data;
- struct bar_tag *next;
- } BAR;
-
- /* */
-
- Is there a simple way to handle operations on both of those lists
- using the same set of functions? Say for adding links we have this
- function:
-
- /* Untested code based on previously written tested code :) */
-
- FOO *sllist_add(FOO *next, int data)
- {
- FOO *temp;
-
- temp = (FOO *)malloc(sizeof(FOO));
-
- if (temp == NULL)
- {
- fprintf(stderr, "\nOut of memory in sllist_add()!\n");
- return (NULL);
- }
-
- temp->data = data;
- temp->next = next;
-
- return (temp);
- }
-
- /* */
-
- I realize I could (feasibly) use void pointers for data and function
- pointers for filling in the data, but that wouldn't really simplify
- things; I might as well just write seperate function libraries for
- each different linked list structure.
-
- My question is if there is a simple (read: elegant) way to handle like
- data structures with unlike data, with a single set of reusable,
- generic, Ansi C functions.
-
-